package cn.com.yitong.demo.demo2.service;

import java.util.List;
import java.util.Map;

import cn.com.yitong.demo.demo2.dao.DemoTable2Dao;
import cn.com.yitong.framework.core.base.service.BaseServiceImpl;
import cn.com.yitong.framework.core.message.vo.ResponseMessageVO;
import cn.com.yitong.framework.core.util.query.Parameters;
import cn.com.yitong.framework.support.context.AresContext;
import cn.com.yitong.framework.util.StrHelper;
import cn.com.yitong.framework.util.TypeHelper;

/**
 * @ClassName: Demo2ServiceImpl
 * @Description: TODO
 * @author: 孙孟（sm@yitong.com.cn）
 * @date: 2015-9-7 上午9:54:59
 */
public class Demo2ServiceImpl extends BaseServiceImpl<Object, DemoTable2Dao> implements Demo2Service {

	/**
	 * sqlKey
	 */
	private final String SQLKEY = "sqlmap.DemoTable2";
	
	/**
	 * @Title: query
	 * @Description: 查询记录并分页
	 * @param context
	 * @param parameters
	 * @return
	 */
	public ResponseMessageVO query(AresContext context, Parameters parameters) {
		//sqlkey
		String sqlKey = SQLKEY+".query";
		//返回响应信息,分页
		return this.getBaseDao().pagedQuery(sqlKey, parameters);
	}
	
	/**
	 * 查询所有记录
	 */
	public ResponseMessageVO queryAll(AresContext context, Parameters parameters) {
		//上下文响应对象
		ResponseMessageVO responseMessageVO = context.getResponseMessage();
		//返回响应信息，所有记录
		List<Map<String,Object>> list = this.getBaseDao().queryAll(parameters);
		responseMessageVO.setResult(list);
		return responseMessageVO;
	}
	
	/**
	 * 插入
	 * @param context
	 * @param parameters
	 */
	public ResponseMessageVO insertData(AresContext context, Map<String,Object> parameters) {
		//上下文响应对象
		ResponseMessageVO response = context.getResponseMessage();
		//主键
		String id = StrHelper.uuid();
		parameters.put("id", id);
		//
		boolean success = this.getBaseDao().insertData(parameters);
		//返回响应信息
		response.setResult(id);
		response.setSuccess(success);
		return response;
	}

	/**
	 * 重写父类insert方法
	 */
	@Override
	public ResponseMessageVO insert(AresContext context, Object parameters) {
		//已知传入类型为Map，转成Map类型
		Map<String, Object> params = TypeHelper.toMap(parameters);
		//上下文响应对象
		ResponseMessageVO response = context.getResponseMessage();
		//主键
		String id = StrHelper.uuid();
		params.put("id", id);
		String sqlKey = SQLKEY+".insert";
		//插入，如果不指定sqlkey，默认根据是否配置实体类自动获取：1.如果为Object则是"insert";2.实体类路径.insert
		boolean success = this.getBaseDao().insert(sqlKey,params);
		//返回响应信息
		response.setResult(id);
		response.setSuccess(success);
		return response;
	}
	
	/**
	 * 删除
	 * @param context
	 * @param id
	 */
	public ResponseMessageVO delete(AresContext context, String id) {
		//上下文响应对象
		ResponseMessageVO response = context.getResponseMessage();
		//执行删除
		String sqlKey = SQLKEY+".delById";
		//修改，如果不指定sqlkey，默认根据是否配置实体类自动获取：1.如果为Object则是"delById";2.实体类路径.delById
		boolean success = this.getBaseDao().delete(sqlKey,id);
		//是否删除成功
		if(!success){
			response.setError("<font color=red>需要删除的数据不存在！</font>");
		}
		//返回响应信息
		response.setSuccess(success);
		return response;
	}
	
	
	/**
	 * 自定义修改方法
	 * @param context
	 * @param parameters
	 * @return
	 */
	public ResponseMessageVO updateById(AresContext context, Map<String,Object> parameters) {
		//上下文响应对象
		ResponseMessageVO response = context.getResponseMessage();
		//
		boolean success = this.getBaseDao().updateById(parameters);
		// 更新记录的ID
		response.setResult(parameters.get("id"));
		//返回响应信息
		response.setSuccess(success);
		return response;
	}
	

	/**
	 * 重写父类update方法
	 */
	@Override
	public ResponseMessageVO update(AresContext context, Object parameters) {
		//已知传入类型为Map，转成Map类型
		Map<String, Object> params = TypeHelper.toMap(parameters);
		//上下文响应对象
		ResponseMessageVO response = context.getResponseMessage();
		// 更新，如果不指定sqlkey，默认根据是否配置实体类自动获取：1.如果为Object则是"update";2.实体类路径.update
		String sqlKey = SQLKEY+".update";
		boolean success = this.getBaseDao().update(sqlKey,params);
		// 更新记录的ID
		response.setResult(params.get("id"));
		//返回响应信息
		response.setSuccess(success);
		return response;
	}
	
}
